package algotithm.weekendrace.leetcode242.test3;

import java.util.TreeSet;

/**
 * @author 25068
 * @Date 2021/5/23 11:17
 */
class Solution {

    public static void main(String[] args) {
//        String s = "01101110";
//        int min = 2;
//        int max = 3;
//        String s = "011010";
//        int min = 2;
//        int max = 3;
//        String s = "01";
//        int min = 1;
//        int max = 1;
        String s = "0000000000";
        int min = 2;
        int max = 5;
        Solution solution = new Solution();
        boolean b = solution.canReach(s, min, max);
        System.out.println(b);
    }

    public boolean canReach(String s, int minJump, int maxJump) {
        char[] c = s.toCharArray();
        int n = c.length;
        if(c[n - 1] != '0') {
            return false;
        }
        //System.out.println();
        TreeSet<Integer> set = new TreeSet<>();
        for(int i = 1; i < n - 1; i++) {
            set.add(i);
        }
        //System.out.println(set);
        for(int i = 0; i < n - 1; i++) {
            if(c[i] != '0' || set.contains(i)) {
                continue;
            }
            if(i + maxJump >= n - 1 && i + minJump <= n - 1) {
                return true;
            }
            int j = i + minJump;
            while(j <= n - 1 && j <= i + maxJump) {
                Integer next = set.ceiling(j);
                if(next == null) {
                    break;
                }
                set.remove(next);
                j = next + 1;
            }
            //System.out.println(set);
        }

        return false;
    }
}
